home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 41.zip / BS1 part 41 / Compute`s Amiga resource 1.adf / Source / ALC / adviff.c < prev    next >
C/C++ Source or Header  |  1989-02-07  |  5KB  |  213 lines

  1. /* These routines are extreme hacks that I haven't had time to revise;
  2.    you're at your own risk in using them! */
  3.  
  4. #include "adviff.h"
  5. #include <stdio.h>
  6. #include <exec/memory.h>
  7. #include <graphics/gfx.h>
  8.  
  9. extern void *allocate_mem();  /* From ADVLASER.C */
  10.  
  11.  
  12.  
  13. read_iff_header(filename, header_struct)
  14. char *filename;
  15. struct iff_header *header_struct;
  16. {
  17.   FILE *file;
  18.  
  19.   if((file = fopen(filename, "r")) == NULL)    /* Open for read-only */
  20.     return(RIFFH_NOOPEN);
  21.  
  22.   if(fread(header_struct, 1, sizeof(struct iff_header), file) <
  23.   sizeof(struct iff_header))
  24.   {
  25.     fclose(file);
  26.     return(RIFFH_INCOMPLETE);
  27.   }
  28.  
  29.   fclose(file);
  30.  
  31.   if(strncmp(header_struct->form, "FORM", 4))
  32.     return(RIFFH_NOFORM);
  33.  
  34.   if(strncmp(header_struct->ilbm, "ILBM", 4))
  35.     return(RIFFH_NOILBM);
  36.  
  37.   return(RIFFH_SUCCESS);
  38. }
  39.  
  40.  
  41.  
  42. load_iff(header, filename, colortable, bitmap)
  43. struct iff_header *header;
  44. char *filename;
  45. WORD colortable[32];
  46. struct BitMap *bitmap;
  47. {
  48.   FILE *file;
  49.   UBYTE *image_buffer;
  50.   int i;
  51.   char in[4];
  52.   ULONG size;
  53.  
  54.   if((file = fopen(filename, "r")) == NULL)    /* Open for read-only */
  55.     return(LIFF_NOOPEN);
  56.  
  57.   /* Set read position to the point right after the iff_header (color data) */
  58.   if(fseek(file, (long)sizeof(struct iff_header), 0) != 0)
  59.   {
  60.     fclose(file);
  61.     return(LIFF_NOSEEK);
  62.   }
  63.  
  64.   /* If color table pointer specified, load it */
  65.   if(colortable != NULL)
  66.     for(i=0; i<header->cmap_size / 3; i++)
  67.     {
  68.       /* Read 3 bytes (red, green, blue) */
  69.       if(fread(in, 1, 3, file) < 3)
  70.       {
  71.         fclose(file);
  72.         return(LIFF_COLORERR);
  73.       }
  74.       colortable[i] =
  75.         ((in[0] & 0xF0) << 4) | (in[1] & 0xF0) | ((in[2] & 0xF0) >> 4);
  76.     }
  77.  
  78.   do  /* Search for the body segment */
  79.   {
  80.     if(fread(in, 1, 4, file) < 4)
  81.     {
  82.       fclose(file);
  83.       return(LIFF_BODYERR);
  84.     }
  85.   } while(strncmp(in, "BODY", 4));
  86.  
  87.   if(fread(&size, 4, 1, file) < 1)
  88.   {
  89.     fclose(file);
  90.     return(LIFF_SIZEERR);
  91.   }
  92.  
  93.   if((image_buffer = allocate_mem(size, MEMF_PUBLIC)) == NULL)
  94.   {
  95.     fclose(file);
  96.     return(LIFF_MEMALLOCERR);
  97.   }
  98.  
  99.   if(fread(image_buffer, 1, (UWORD)size, file) < (UWORD)size)
  100.   {
  101.     fclose(file);
  102.     return(LIFF_IMAGEREADERR);
  103.   }
  104.  
  105.   if(get_data(image_buffer, header, bitmap) == FALSE)
  106.   {
  107.     fclose(file);
  108.     return(LIFF_GETDATAERR);
  109.   }
  110.  
  111.   if(image_buffer != NULL) FreeMem(image_buffer, size);
  112.   fclose(file);
  113.   return(LIFF_SUCCESS);
  114. }
  115.  
  116.  
  117.  
  118. int get_data(image_buffer, iffheader, bitmap)
  119. UBYTE *image_buffer;
  120. struct iff_header *iffheader;
  121. struct BitMap *bitmap;
  122. {
  123.   int status;
  124.  
  125.   if(iffheader->compression == 0)
  126.     status = read_raw_iff(image_buffer, iffheader, bitmap);
  127.   else
  128.     status = iff_read(image_buffer, iffheader, bitmap);
  129.   return(status);
  130. }
  131.  
  132.  
  133. read_raw_iff(image_buffer, iffheader, bitmap)
  134. UBYTE *image_buffer;
  135. struct iff_header *iffheader;
  136. struct BitMap *bitmap;
  137. {
  138.   return(FALSE);  /* NOT SUPPORTED! */
  139. }
  140.  
  141.  
  142.  
  143. iff_read(image_buffer, iffheader, bitmap)
  144. UBYTE *image_buffer;
  145. struct iff_header *iffheader;
  146. struct BitMap *bitmap;
  147. {
  148.   int row, plane;
  149.  
  150.   for(row=0; row<iffheader->height; row++)
  151.     for(plane=0; plane<iffheader->num_planes; plane++)
  152.       decompress(image_buffer, iffheader, bitmap, row, plane);
  153. }
  154.  
  155.  
  156.  
  157. decompress(image_buffer, iffheader, bitmap, row, plane)
  158. UBYTE *image_buffer;
  159. struct iff_header *iffheader;
  160. struct BitMap *bitmap;
  161. unsigned int row, plane;
  162. {
  163.   register int i;
  164.   register unsigned int offset, offinc;
  165.   static int dccount, dcptr, ffbytes, image_index;
  166.   static char decombuf[160], comp_flg, comp_byte;
  167.  
  168.   dccount= (( (iffheader->width + 15) >> 4 ) << 1);
  169.  
  170.   dcptr = 0;
  171.   while(dccount)
  172.   {
  173.     comp_flg = image_buffer[image_index++];
  174.     if(comp_flg >= 0)
  175.     {
  176.       comp_flg++;
  177.       dccount -= comp_flg;
  178.       for(i=0; i<comp_flg; i++)
  179.         decombuf[dcptr++] = image_buffer[image_index++];
  180.     }
  181.     else
  182.     if(comp_flg != -128)
  183.     {
  184.       comp_flg = -comp_flg+1;
  185.       dccount -= comp_flg;
  186.       comp_byte = image_buffer[image_index++];
  187.       for(i=0; i<comp_flg; ++i)
  188.         decombuf[dcptr++] = comp_byte;
  189.     }
  190.   }
  191.   tobitmap(bitmap, iffheader, row, plane, decombuf);
  192.   return(TRUE);
  193. }
  194.  
  195.  
  196.  
  197. tobitmap(bitmap, iffheader, row, plane, decombuf)
  198. struct BitMap *bitmap;
  199. struct iff_header *iffheader;
  200. UWORD row, plane;
  201. char *decombuf;
  202. {
  203.   UWORD ix, bytewidth, yoffset;
  204.  
  205.   bytewidth = ((iffheader->width + 15) >> 4) << 1;
  206.   yoffset = row * bytewidth;
  207.   for(ix=0; ix<bytewidth; yoffset++)
  208.   {
  209.     *(bitmap->Planes[plane] + yoffset + (ix >> 1)) = decombuf[ix++];
  210.     *(bitmap->Planes[plane] + yoffset + (ix >> 1)) = decombuf[ix++];
  211.   }
  212. }
  213.